﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;

namespace Enter.Utils.JsonExt
{
    public class DateFormatConverter : IsoDateTimeConverter
    {
        public const string Format_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
        public const string Format_yyyyMMdd = "yyyy-MM-dd";
        public DateFormatConverter()
        {
            base.DateTimeFormat = Format_yyyyMMdd;
        }
        public DateFormatConverter(string format)
        {
            base.DateTimeFormat = format;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value == null || (DateTime)value == DateTime.MinValue)
            {
                writer.WriteValue(string.Empty);
            }
            else
            {
                base.WriteJson(writer, value, serializer);
            }
        }
    }
}
